package com.climber.spb_rabbitmq.spb_delayqueue_ttl.config;

import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

/**
 * @author frx
 * @version 1.0
 * @date 2022/7/25  22:31
 * desc:TTL队列 配置文件类代码
 */
@Configuration
public class RabbitMQConfig {

    public static final String X_EXCHANGE="X";
    public static final String Y_DEAD_LETTER_EXCHANGE = "deadExchange";
    public static final String Y_DEAD_LETTER_Queue= "deadQueue";
    public static final String QUEUE_C = "QC";
    public static final String ROUTING_KEY = "XC";

    @Bean("xExchange")
    public DirectExchange xExchange(){
        return new DirectExchange(X_EXCHANGE,false,false);
    }

    @Bean("deadExchange")
    public DirectExchange deadExchange(){
        return new DirectExchange(Y_DEAD_LETTER_EXCHANGE,false,false);
    }

    //声明队列 C 死信交换机
    @Bean("queueC")
    public Queue queueB(){
        Map<String, Object> args = new HashMap<>(3);
        args.put("x-dead-letter-exchange", Y_DEAD_LETTER_EXCHANGE);
        args.put("x-dead-letter-routing-key", "YD");
        return QueueBuilder.nonDurable(QUEUE_C).withArguments(args).build();
    }
    //声明队列 B 绑定 X 交换机
    @Bean
    public Binding queueBindingX(@Qualifier("queueC") Queue queueC,
                                  @Qualifier("xExchange") DirectExchange xExchange){
        return BindingBuilder.bind(queueC).to(xExchange).with(ROUTING_KEY);
    }

    @Bean("yExchange")
    public DirectExchange yExchange(){
        return new DirectExchange(Y_DEAD_LETTER_EXCHANGE,false,false);
    }
    @Bean("queueD")
    public Queue queueD(){
        return QueueBuilder.nonDurable(Y_DEAD_LETTER_Queue).build();
    }

    //声明队列 QD 绑定 Y 交换机
    @Bean
    public Binding queueDBindingY(@Qualifier("queueD") Queue queueD,
                                  @Qualifier("yExchange") DirectExchange yExchange){
        return BindingBuilder.bind(queueD).to(yExchange).with("YD");
    }

}
